/*
 * Compiler.h
 *
 *  Created on: 2016年8月31日
 *      Author: wll
 */

#ifndef INCLUDE_COMPILER_H_
#define INCLUDE_COMPILER_H_

#include "CompilerId.h"
#include "LanguageGrammar.h"
#include "LanguageParsers.h"
#include "WllInterpreter.h"
#include "Strategy.h"
#include <iostream>
#include <vector>
#include <string>
#include "CompilerManager.h"
#include "SmartPointer.h"

static const char* GRAMMAR_FILE_NAME = "data/grammar";

class WllInterpreter;
class LanguageParsers;
//前置声明CompilerManager,因为有循环include
class CompilerManager;

class Compiler
{
public:
    //构建空的compiler
	Compiler();
    //析构函数
    ~Compiler();
public:
    //加载wll语言文法
	virtual bool LoadLanguage();
    bool LoadLanguage(const string &grammar_file_name);
public:
    virtual bool Process(std::string& input_file_name, std::string& output_file_name);
	virtual bool Process(std::istream& inf, std::ostream& outf);
    virtual bool Process(std::vector<Symbols>& input_symbols, std::vector<Symbols>& output_symbols);
	virtual bool Process(SymbolsFlow &input_symbols, std::vector<Symbols>& output_symbols, Symbols start_symbol);
public:
	//for debug display languages
	virtual void DisplayLanguage(ostream& o);
    //序列化语法规则
    void DumpLanguage(const std::string& file_name);
public:
	LanguageGrammar languages;
	//Strategy<WllLoader> loader_strategy;
	//Strategy<SymbolsFlow> flow_strategy;
    Strategy<LanguageParsers> parser_strategy;
	WllInterpreter* interpreter;

    CompilerId compiler_id;
    std::string processing_file_name;
    //不从单例获取指针，避免构造阶段死锁
    CompilerManager* compiler_manager;
public:
    static Compiler* Load(const CompilerId *compiler_id);
    void Save(const string &file_name);
};

#endif /* INCLUDE_COMPILER_H_ */
